import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.mysql.jdbc.ResultSet;

/**
 * This class is the check assignment UI
 */
public class CheckAssUI extends JPanel {
	/**
	 * title for the current window  
	 */
	private JLabel checkTitle = null;
	/**
	 * label for no assignment message  
	 */
	private JLabel noassnmsg = null;
	/**
	 * back button 
	 */
	public JButton btnBack = null;
	/**
	 * help button 
	 */
	public JButton btnHelp = null;
	/**
	 * save the current panel for the "back" function 
	 */
	private JPanel curPanel;
	/**
	 *   course id
	 */
	private String courseID;
	/**
	 *   student id
	 */
	private String studentID;
	/**
	 *   assignment number 
	 */
	private Integer assNum;
	/**
	 *  array for the submitted assignment 
	 */
	private ArrayList<Sub> subList = new ArrayList<Sub>();
	/**
	 *  the loaded file
	 */
	private MyFile file;
	/**
	 *  logout button
	 */
	public JButton btnLogout = null;
	/**
	 *  i for index
	 */
	int i;
	/**
	* Construct
	*/
	public CheckAssUI () {
		super();
		emptyinitialize();
	}
	/**
	* Construct
	* 
	*  @param courseID to set the id course
	*  
	*  @param assNum to set the assignment number
	*/
	public CheckAssUI(String courseID, Integer assNum) {
		
		  super();
		  this.courseID = courseID;
		  this.assNum = assNum;
		  initialize();
	}

	private void initialize() {
		curPanel = this;
		checkTitle = new JLabel("Check Assignment");
		checkTitle.setBounds(180, 28, 150, 14);

		this.setSize(400, 321);
		this.setLayout(null);
		
		this.add(checkTitle);
		Sub submisstion = new Sub (courseID,assNum,Global.logIn.getUserlogin());
		SQL s1 = new SQL (submisstion,48);
		s1.accept();
		String date = (String)s1.getMsg();
		String split = "\\.";
		String[] finalDate = date.split(split);
		for (int j=0;j<finalDate.length;j++)
			System.out.println("date is "+finalDate[j]);
		SQL s2 = new SQL (submisstion,44);
		s2.accept();
		subList = (ArrayList<Sub>)s2.getMsg();
		int size = subList.size();
		
		JButton[] b=new JButton[size];
		JLabel[] lb=new JLabel[size];
		JLabel[] lb2=new JLabel[size];
			int x=270, y=60, width=150, height=20, width1=90;
			int x1=20, x2=110;
			for (i=0; i<size ; i++)
			{
				System.out.println("student id is" +(subList.get(i)).getStudentID()); 
				SQL s3 = new SQL ((subList.get(i)).getStudentID(),40);
				s3.accept();
				Object msg = s3.getMsg();
				lb[i] = new JLabel((subList.get(i)).getStudentID());
				lb[i].setBounds(x1, y, width, height);
				curPanel.add(lb[i]);
				String[] subDate = (subList.get(i).getDate()).split(split);
				for (int j=0;j<subDate.length;j++)
					System.out.println("date is "+subDate[j]);
				if(
						( subDate[2].compareTo(finalDate[2]) <0)  ||
						((subDate[2].compareTo(finalDate[2])==0)  &&  (subDate[1].compareTo(finalDate[1]) <0)) ||
						((subDate[0].compareTo(finalDate[0]) <0)  &&  (subDate[1].compareTo(finalDate[1])==0 ) &&  (subDate[2].compareTo(finalDate[2])==0))
																																								)
							lb2[i] = new JLabel("In time? - yes");
				else 
							lb2[i] = new JLabel("In time? - no");
				lb2[i].setBounds(x2, y, width, height);
				curPanel.add(lb2[i]);
				b[i] = new JButton("Open");
				curPanel.add(b[i]);
				b[i].setBounds(x-20, y, width1, height);
				b[i].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						Save save = new Save((subList.get(i-1)).getSubFile());
						save.saveFile((subList.get(i-1)).getSubFile());
					}
				});
				this.add(b[i],null);
				this.setVisible(true);
				

				b[i] = new JButton("Submit");
				curPanel.add(b[i]);
				b[i].setBounds(x+80, y, width1, height);
				b[i].addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						ReturnCheckAssUI returnCheckAssUI = new ReturnCheckAssUI(subList.get(i-1));
						setVisible(false);
						System.out.println("Check Assignments");
						Global.main.setContentPane(returnCheckAssUI);
						returnCheckAssUI.btnBack.addActionListener(new ActionListener() {   
							public void actionPerformed(ActionEvent arg0) {
								Global.main.setContentPane(curPanel);
								curPanel.setVisible(true);
							}
						});	
					}
				});
				this.add(b[i],null);
				this.setVisible(true);
				y+=30;
				
			}
	
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		/**
		 *  logout button Implementation
		 */
		btnLogout = new JButton("Logout");        
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		/**
		 *  help button Implementation
		 */
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose a Course and and click on ", "'Open Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		this.add(btnHelp,null);

	}
	
	private void emptyinitialize() {
		curPanel = this;

		checkTitle = new JLabel("Check Assinment");
		checkTitle.setBounds(98, 24, 154, 25); 
		this.add(noassnmsg);
		
		noassnmsg = new JLabel("No Submisstion for this Assignment");
		noassnmsg.setBounds(133, 49, 170, 46);

		this.setSize(400, 321);
		this.setLayout(null);
		
		this.add(checkTitle);
	
		btnBack = new JButton("Back");
		btnBack.setBounds(187, 253, 86, 25);
		this.add(btnBack,null);
		this.setVisible(true);
		/**
		 *  logout button Implementation
		 */
		btnLogout = new JButton("Logout");        
		btnLogout.setBounds(10, 11, 80, 19);
		add(btnLogout);
		btnLogout.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  LogoutWin logt = new LogoutWin();
				  System.out.println("user press Logout button");
			}
		});
		/**
		 *  help button Implementation
		 */
		btnHelp = new JButton("Help");
		btnHelp.setBounds(390, 11, 80, 19);
		btnHelp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				  HelpWin help = new HelpWin ("Choose a Course and and click on ", "'Open Assignment' button");
				  System.out.println("user press Help button");
			}
		});
		
	}
}